package com.coffee.admin.controller.supplier;

import com.alibaba.fastjson.JSONObject;
import com.coffee.admin.api.supplier.query.SupplierRoleQuery;
import com.coffee.admin.api.supplier.rep.SupplierOperateRes;
import com.coffee.admin.api.supplier.req.SupplierAccountAddReq;
import com.coffee.admin.api.supplier.req.SupplierAccountListReq;
import com.coffee.admin.api.supplier.req.SupplierAccountUpdateReq;
import com.coffee.admin.api.supplier.req.SupplierUpdateIsEnableReq;
import com.coffee.admin.api.supplier.result.SupplierRoleResult;
import com.coffee.admin.api.supplier.service.SupplierRoleService;
import com.coffee.admin.api.supplier.service.SupplierService;
import com.coffee.admin.controller.BaseController;
import com.coffee.core.anotation.OperLog;
import com.coffee.core.anotation.SupplierLoginCheck;
import com.coffee.core.entity.LoginUser;
import com.coffee.core.entity.Page;
import com.coffee.core.entity.PagerInfo;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.result.Result;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@RestController
@RequestMapping("/supplier/account")
public class SupplierAccountController extends BaseController {

    @DubboReference(check = false)
    private SupplierRoleService supplierRoleService;

    @DubboReference(check = false)
    private SupplierService supplierService;

    @OperLog(operModul = "供应商-系统管理-账户管理", operDesc = "分页查询账户管理列表")
    @SupplierLoginCheck(alias = "SupplierAccountListAlias")
    @RequestMapping(value = "/findList", method = {RequestMethod.POST})
    public Result<Page<SupplierOperateRes>> findList(HttpServletRequest request, @RequestBody SupplierAccountListReq params) {
        LoginUser supplier = getSupplier();
        Result<Page<SupplierOperateRes>> result = Result.success(null);
        PagerInfo pagerInfo = setDefaultPageInfo(params.getPageSize(), params.getPageIndex());
        params.setSupplierId(supplier.getSupplierId());
        try {
            result.setData(supplierService.findMerchantOperatorListByPage(params, pagerInfo));
        } catch (BusinessException e) {
            return Result.failed(e.getCode(), e.getMessage());
        }
        return result;
    }

    @OperLog(operModul = "供应商-系统设置-账户管理", operDesc = "获取账户详情")
    @SupplierLoginCheck(alias = "SupplierAccountListAlias")
    @RequestMapping(value = "/detail")
    public Result<SupplierOperateRes> detail(@RequestBody SupplierAccountListReq params) {
        Result<SupplierOperateRes> result = Result.success(null);
        result.setData(supplierService.findMerchantOperatorDetail(params));
        return result;
    }

    @OperLog(operModul = "供应商-系统管理-账户管理", operDesc = "新增账户")
    @SupplierLoginCheck(alias = "SupplierAccountOperationAlias")
    @RequestMapping(value = "/add", method = {RequestMethod.POST})
    public Result<String> save(@RequestBody SupplierAccountAddReq params) {
        Result<String> result = Result.success(null);
        try {
            params.setSupplierId(getSupplier().getSupplierId());
            supplierService.insertSupplierOperate(params);
        } catch (BusinessException e) {
            return Result.failed(e.getCode(), e.getMessage());
        }
        return result;
    }

    @OperLog(operModul = "供应商-系统管理-账户管理", operDesc = "修改账户")
    @SupplierLoginCheck(alias = "SupplierAccountOperationAlias")
    @RequestMapping(value = "/update", method = {RequestMethod.POST})
    public Result<String> update(@RequestBody SupplierAccountUpdateReq params) {
        Result<String> result = Result.success(null);
        try {
            supplierService.updateSupplierOperate(params);
        } catch (BusinessException e) {
            return Result.failed(e.getCode(), e.getMessage());
        }
        return result;
    }

    @OperLog(operModul = "供应商-系统管理-账户管理", operDesc = "启用禁用登录账户")
    @SupplierLoginCheck(alias = "SupplierAccountOperationAlias")
    @RequestMapping(value = "/updateIsEnable", method = {RequestMethod.POST})
    public Result<String> updateIsEnable(@RequestBody JSONObject json) {
        Result<String> result = Result.success(null);

        Integer id = json.getInteger("id");
        String isEnable = json.getString("isEnable");
        SupplierUpdateIsEnableReq supplierUpdateIsEnableReq = new SupplierUpdateIsEnableReq();
        supplierUpdateIsEnableReq.setId(id);
        supplierUpdateIsEnableReq.setIsEnable(isEnable);
        try {
            supplierService.updateIsEnable(supplierUpdateIsEnableReq);
        } catch (BusinessException e) {
            return Result.failed(e.getCode(), e.getMessage());
        }
        return result;
    }

    @OperLog(operModul = "供应商-系统管理-账户管理", operDesc = "删除账户")
    @SupplierLoginCheck(alias = "SupplierAccountOperationAlias")
    @RequestMapping(value = "/delete", method = {RequestMethod.POST})
    public Result<String> deleteOperate(@RequestBody JSONObject json) {
        Result<String> result = Result.success(null);
        Integer id = json.getInteger("id");
        supplierService.delete(id);
        return result;
    }

    @OperLog(operModul = "供应商-系统管理-账户管理", operDesc = "查询角色列表")
    @SupplierLoginCheck(alias = "SupplierAccountListAlias")
    @RequestMapping(value = "/findRoleList", method = {RequestMethod.POST})
    public Result<List<SupplierRoleResult>> findRoleList(HttpServletRequest request) {
        Result<List<SupplierRoleResult>> result = Result.success(null);
        SupplierRoleQuery param = new SupplierRoleQuery();
        param.setIsAdmin("N");
        param.setSupplierId(getSupplier().getSupplierId());
        result.setData(supplierRoleService.getList(param));
        return result;
    }
}
